package com.example.choupaiapp.main.activity.find;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.widget.NestedScrollView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.method.MovementMethod;
import android.text.style.DynamicDrawableSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.choupaiapp.R;
import com.example.choupaiapp.common.adapter.BaseAdapterHelper;
import com.example.choupaiapp.common.adapter.QuickAdapter;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.ContextUtil;
import com.example.choupaiapp.common.util.DateFormatUtil;
import com.example.choupaiapp.common.util.DialogUtil;
import com.example.choupaiapp.common.util.GsonUtil;
import com.example.choupaiapp.common.util.SharedPreferencesHelp;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.common.view.XListView;
import com.example.choupaiapp.common.view.statusbar.StatusView;
import com.example.choupaiapp.main.activity.ChoseLoginActivity;
import com.example.choupaiapp.main.activity.home.CommentDetailActivity;
import com.example.choupaiapp.main.activity.home.OtherInfoActivity;
import com.example.choupaiapp.main.activity.mine.ReportActivity;
import com.example.choupaiapp.main.bean.CommentListBean;
import com.example.choupaiapp.main.bean.CommentRemindJsonBean;
import com.example.choupaiapp.main.bean.FindDetailBean;
import com.example.choupaiapp.main.bean.RemindJson;
import com.example.choupaiapp.main.bean.SendCodeBean;
import com.example.choupaiapp.main.bean.ShareFindBean;
import com.example.choupaiapp.main.bean.UserInfoBean;
import com.example.choupaiapp.main.callback.WechatCallBack;
import com.example.choupaiapp.main.constant.Config;
import com.example.choupaiapp.main.constant.Url;
import com.example.choupaiapp.main.util.DetailCommentDialog;
import com.example.choupaiapp.main.util.DialogFragmentDataCallback;
import com.example.choupaiapp.main.util.WXShareUtil;
import com.example.choupaiapp.main.util.WechatLoginUtil;
import com.example.choupaiapp.main.view.ScaleImageView;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.shuyu.textutillib.RichTextView;
import com.shuyu.textutillib.SmileUtils;
import com.shuyu.textutillib.TextCommonUtils;
import com.shuyu.textutillib.listener.ITextViewShow;
import com.shuyu.textutillib.listener.SpanAtUserCallBack;
import com.shuyu.textutillib.listener.SpanTopicCallBack;
import com.shuyu.textutillib.listener.SpanUrlCallBack;
import com.shuyu.textutillib.model.TopicModel;
import com.shuyu.textutillib.model.UserModel;
import com.shuyu.textutillib.span.ClickAtUserSpan;
import com.shuyu.textutillib.span.ClickTopicSpan;
import com.shuyu.textutillib.span.LinkSpan;
import com.sina.weibo.sdk.WbSdk;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.share.WbShareCallback;
import com.sina.weibo.sdk.share.WbShareHandler;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ASUS on 2019/12/2.
 */

public class FindDetailActivity extends CommonActivity implements OnRefreshLoadMoreListener, WbShareCallback, WechatCallBack, DialogFragmentDataCallback {

    @BindView(R.id.find_bg)
    ScaleImageView findBg;
    @BindView(R.id.status_view)
    StatusView statusView;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.head_photo)
    CircleImageView headPhoto;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.follow)
    TextView follow;
    @BindView(R.id.title_rl)
    RelativeLayout titleRl;
    @BindView(R.id.comment_et)
    RichTextView commentEt;
    @BindView(R.id.comment_rl)
    RelativeLayout commentRl;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.listview)
    XListView listview;
    private UserInfoBean mUserInfoBean;
    private int pageNum = 1;
    private int pageSize = 10;
    private QuickAdapter<CommentListBean.PageBean.ListBean> quickAdapter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.praise_num)
    TextView praise_num;
    @BindView(R.id.comment_num)
    TextView comment_num;
    @BindView(R.id.share_num)
    TextView share_num;
    @BindView(R.id.comment_num_tv)
    TextView comment_num_tv;
    @BindView(R.id.image)
    CircleImageView image;
    StyleSpan span = new StyleSpan(Typeface.BOLD);
    StyleSpan span2 = new StyleSpan(Typeface.BOLD);
    StyleSpan span1 = new StyleSpan(Typeface.NORMAL);
    StyleSpan span3 = new StyleSpan(Typeface.NORMAL);
    private Drawable drawable_select;
    private Drawable drawable_unselect;
    private String et_flag = "comment";
    private String replyUserId;
    private String replyCommentId;
    @BindView(R.id.praise_iv)
    ImageView praiseIv;
    private Tencent mTencent;
    private String id;
    private FindDetailBean mFindDetailBean;
    @BindView(R.id.webview)
    WebView webview;
    private DetailCommentDialog commentDialogFragment;
    @BindView(R.id.praise)
    LinearLayout praise;
    private int position;
    private String categoriesId;
    private String type;
    private String replayName = "";
    private String replayContent = "";
    private List<UserModel> nameList;
    private int replyPosition;
    private String flag = "";
    private boolean isFirst = false;

    private ITextViewShow getIText(TextView itext) {
        ITextViewShow iTextViewShow = new ITextViewShow() {
            @Override
            public void setText(CharSequence charSequence) {
                itext.setText(charSequence);
            }

            @Override
            public CharSequence getText() {
                return itext.getText();
            }

            @Override
            public void setMovementMethod(MovementMethod movementMethod) {
                itext.setMovementMethod(movementMethod);
            }

            @Override
            public void setAutoLinkMask(int flag) {
                itext.setAutoLinkMask(flag);
            }


            @Override
            public ClickAtUserSpan getCustomClickAtUserSpan(Context context, UserModel userModel, int color, SpanAtUserCallBack spanClickCallBack) {
                return null;
            }

            @Override
            public ClickTopicSpan getCustomClickTopicSpan(Context context, TopicModel topicModel, int color, SpanTopicCallBack spanTopicCallBack) {
                return null;
            }

            @Override
            public LinkSpan getCustomLinkSpan(Context context, String url, int color, SpanUrlCallBack spanUrlCallBack) {
                return null;
            }

            @Override
            public int emojiSize() {
                return 0;
            }

            @Override
            public int verticalAlignment() {
                return DynamicDrawableSpan.ALIGN_BOTTOM;
            }
        };
        return iTextViewShow;
    }

    SpanAtUserCallBack spanAtUserCallBack = new SpanAtUserCallBack() {
        @Override
        public void onClick(View view, UserModel userModel1) {
            Bundle bundle = new Bundle();
            bundle.putString("userId", userModel1.getUser_id());
            bundle.putString("userName", userModel1.getUser_name());
            startActivity(OtherInfoActivity.class, bundle);
            if (view instanceof TextView) {
                ((TextView) view).setHighlightColor(Color.TRANSPARENT);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.activity_find_detail);
    }

    @Override
    public String getCommentText() {
        return commentEt.getText().toString();
    }

    @Override
    public String getReplayName() {
        return replayName;
    }

    @Override
    public String getReplayContent() {
        return replayContent;
    }

    @Override
    public void setCommentText(String commentTextTemp) {
        if (!TextUtils.isEmpty(commentTextTemp)) {
            commentEt.setRichText(commentTextTemp);
        } else {
            commentEt.setText(commentTextTemp);
        }
    }

    @Override
    public List<UserModel> getNameList() {
        for (int i = 0; i < commentEt.getNameList().size(); i++) {
            commentEt.getNameList().get(i).setUser_name("@" + commentEt.getNameList().get(i).getUser_name());
        }
        return commentEt.getNameList();
    }

    @Override
    public void setNameList(List<UserModel> nameList) {
        commentEt.setNameList(nameList);
    }

    private void tokenVerify() {
        HttpParams params = new HttpParams();
        params.put("token", (String) SharedPreferencesHelp.get("token", ""));
        finalOkGo.requestHead(RequestType.POSTHEAD, Url.VERIFY_TOKEN, params, Url.VERIFY_TOKEN, new Callback<SendCodeBean>() {
            @Override
            public void onSuccess(SendCodeBean sendCodeBean) {
                if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                    commentDialogFragment = new DetailCommentDialog();
                    commentDialogFragment.setCommentFlag("FindDetailActivity");
                    commentDialogFragment.show(getFragmentManager(), "DetailCommentDialog");
                }
            }

            @Override
            public void onError(Response response) {
                super.onError(response);
            }
        });
    }

    /**
     * 处理自己的表情
     */
    private void initEmoji() {
        List<Integer> data = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        int resofficial = getResources().getIdentifier("icon_comment_official", "mipmap", getPackageName());
        int resgod = getResources().getIdentifier("icon_comment_god", "mipmap", getPackageName());
        data.add(resofficial);
        strings.add("[icon_comment_official]");
        data.add(resgod);
        strings.add("[icon_comment_god]");
        /**初始化为自己的**/
        SmileUtils.addPatternAll(SmileUtils.getEmoticons(), strings, data);
    }

    @Override
    protected void initView() {

        EventBus.getDefault().register(this);

        drawable_select = getResources().getDrawable(R.mipmap.icon_praise_select);
        drawable_unselect = getResources().getDrawable(R.mipmap.item_praise);

        initEmoji();

        mUserInfoBean = (UserInfoBean) SharedPreferencesHelp.getBean("userInfo");

        if (mUserInfoBean != null) {
            Glide.with(context)
                    .load(mUserInfoBean.getUser().getAvatar() + Config.IMAGE_LIM)
                    .error(R.mipmap.icon_no_head)
                    .into(image);
        } else {
            Glide.with(context)
                    .load(R.mipmap.icon_no_head)
                    .error(R.mipmap.icon_no_head)
                    .into(image);
        }

        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));
        refreshLayout.setEnableAutoLoadMore(true);

        nameList = new ArrayList<>();
        //直接使用RichTextView
        commentEt.setAtColor(Color.parseColor(getString(R.string.remind_color)));
        commentEt.setLinkColor(Color.BLUE);
        //所有配置完成后才设置text
        commentEt.setNameList(nameList);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            id = bundle.getString("id");
            categoriesId = bundle.getString("categoriesId");
            type = bundle.getString("type");
            position = bundle.getInt("position");
            flag = bundle.getString("flag");
        }

//        WebSettings webSettings = webview.getSettings();
//        webSettings.setJavaScriptEnabled(true);
//        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView nestedScrollView, int i, int i1, int i2, int i3) {
                if (i1 <= 0) {
                    name.setTextColor(getResources().getColor(R.color.white));
                    time.setTextColor(getResources().getColor(R.color.white));
                    titleRl.setBackgroundColor(getResources().getColor(R.color.transparent));
                    statusView.setBackgroundColor(getResources().getColor(R.color.transparent));
                } else if (i1 > 0 && i1 < findBg.getHeight() / 2) {
                    float f = (float) i1 / (float) findBg.getHeight();
                    float alpha = (float) 255 * f * 2;
                    name.setTextColor(Color.argb((int) alpha, 0, 0, 0));
                    time.setTextColor(Color.argb((int) alpha, 0, 0, 0));
                    titleRl.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
                    statusView.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
//                    statusView.setBackgroundColor(Color.argb((int) alpha, 204, 204, 204));
                } else {
                    name.setTextColor(getResources().getColor(R.color.color_333));
                    time.setTextColor(getResources().getColor(R.color.color_333));
                    titleRl.setBackgroundColor(getResources().getColor(R.color.white));
                    statusView.setBackgroundColor(getResources().getColor(R.color.white));
//                    statusView.setBackgroundColor(getResources().getColor(R.color.normal_status));
                }
            }
        });

        quickAdapter = new QuickAdapter<CommentListBean.PageBean.ListBean>(this, R.layout.item_comment_receive) {
            @Override
            protected void convert(BaseAdapterHelper helper, CommentListBean.PageBean.ListBean item) {
                LinearLayout item_ll = helper.getView(R.id.item_ll);
                item_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (Utils.isDoubleClick()) {
                            return;
                        }
                        if (mUserInfoBean != null && mFindDetailBean != null) {
                            replyPosition = helper.getPosition();
                            if ((mUserInfoBean.getUser().getUserId() + "").equals(mFindDetailBean.getDiscover().getUserId() + "")) {
                                deleteComment(item, "delete_report");
                            } else if ((mUserInfoBean.getUser().getUserId() + "").equals(item.getUserId() + "")) {
                                deleteComment(item, "delete");
                            } else {
                                deleteComment(item, "report");
                            }
                        } else {
                            startActivity(ChoseLoginActivity.class);
                        }

                    }
                });

                item_ll.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        return true;
                    }
                });

                CircleImageView head_photo = helper.getView(R.id.head_photo);
                Glide.with(context)
                        .load(item.getAvatar() + Config.IMAGE_LIM)
                        .dontAnimate()
                        .placeholder(R.mipmap.icon_no_head)
                        .into(head_photo);
                head_photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (Utils.isDoubleClick()) {
                            return;
                        }
                        Bundle bundle1 = new Bundle();
                        bundle1.putString("userId", item.getUserId() + "");
                        bundle1.putString("userName", item.getNickname() + "");
                        startActivity(OtherInfoActivity.class, bundle1);
                    }
                });

                helper.setText(R.id.name, item.getNickname());

                RichTextView richTextView = helper.getView(R.id.content);
                List<UserModel> list = new ArrayList<>();
                if (!TextUtils.isEmpty(item.getRemindJson()) && !"[]".equals(item.getRemindJson())) {
                    List<RemindJson> remindJsons = GsonUtil.jsonToList(item.getRemindJson(), RemindJson.class);
                    if (remindJsons != null) {
                        for (int i = 0; i < remindJsons.size(); i++) {
                            UserModel userModel = new UserModel();
                            userModel.setUser_name(remindJsons.get(i).getNickname());
                            userModel.setUser_id(remindJsons.get(i).getUserId() + "");
                            list.add(userModel);
                        }
                    }
                }
                //直接使用RichTextView
                richTextView.setAtColor(Color.parseColor(getString(R.string.remind_color)));
                richTextView.setLinkColor(Color.BLUE);
                richTextView.setSpanAtUserCallBackListener(spanAtUserCallBack);
                //所有配置完成后才设置text
                if (item.getWeight() == 1) {
                    richTextView.setRichTextUser("[icon_comment_official] " + item.getComment(), list);
                } else if (item.getWeight() == -2) {
                    richTextView.setRichTextUser("[icon_comment_god] " + item.getComment(), list);
                } else {
                    richTextView.setRichTextUser(item.getComment(), list);
                }

                TextView praise = helper.getView(R.id.praise);
                if (item.getIsLike() == 0) {
                    praise.setCompoundDrawablesWithIntrinsicBounds(drawable_unselect, null, null, null);
                } else {
                    praise.setCompoundDrawablesWithIntrinsicBounds(drawable_select, null, null, null);
                }
                praise.setText(item.getLikeSum() + "");
                praise.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (Utils.isDoubleClick()) {
                            return;
                        }
                        if (item.getIsLike() == 1) {
                            praise.setEnabled(false);
                            showDialog(context, "取消中...");
                            JSONObject jsonObject = new JSONObject();
                            try {
                                jsonObject.put("contentId", item.getId());
                                jsonObject.put("module", 2);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.COMMENT_CANCEL_PRAISE, null, jsonObject.toString(), new Callback<SendCodeBean>() {
                                @Override
                                public void onSuccess(SendCodeBean sendCodeBean) {
                                    dismissDialog();
                                    praise.setEnabled(true);
                                    if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                        item.setIsLike(0);
                                        item.setLikeSum(item.getLikeSum() - 1);
                                        quickAdapter.notifyDataSetChanged();
                                    } else {
                                        if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                        } else {
                                            showImageToas(sendCodeBean.getMsg());
                                        }
                                    }
                                }

                                @Override
                                public void onError(Response<String> response) {
                                    super.onError(response);
                                    dismissDialog();
                                    praise.setEnabled(true);
                                }
                            });
                        } else {
                            praise.setEnabled(false);
                            showDialog(context, "点赞中...");
                            JSONObject jsonObject = new JSONObject();
                            try {
                                jsonObject.put("contentId", item.getId());
                                jsonObject.put("module", 2);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.COMMENT_PRAISE, null, jsonObject.toString(), new Callback<SendCodeBean>() {
                                @Override
                                public void onSuccess(SendCodeBean sendCodeBean) {
                                    dismissDialog();
                                    praise.setEnabled(true);
                                    if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                        item.setIsLike(1);
                                        item.setLikeSum(item.getLikeSum() + 1);
                                        quickAdapter.notifyDataSetChanged();
                                    } else {
                                        if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                        } else {
                                            showImageToas(sendCodeBean.getMsg());
                                        }
                                    }
                                }

                                @Override
                                public void onError(Response<String> response) {
                                    super.onError(response);
                                    dismissDialog();
                                    praise.setEnabled(true);
                                }
                            });
                        }
                    }
                });


                TextView comment = helper.getView(R.id.comment);
                comment.setText(item.getCommentSum() + "");
                comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (Utils.isDoubleClick()) {
                            return;
                        }
                        replyUserId = item.getUserId() + "";
                        replyCommentId = item.getId() + "";
                        et_flag = "receive";
                        replayName = item.getNickname();
                        replayContent = item.getComment();
                        replyPosition = helper.getPosition();
                        tokenVerify();

                    }
                });

                TextView time = helper.getView(R.id.time);
                timecha(item.getCreateTime(), time);

                LinearLayout receive_ll = helper.getView(R.id.receive_ll);
                TextView more = helper.getView(R.id.more);
                if (item.getLowerComment() != null && item.getLowerComment().size() != 0) {
                    if (item.getCommentSum() > 2) {
                        more.setVisibility(View.VISIBLE);
                        more.setText("查看全部" + item.getCommentSum() + "条回复");
                    } else {
                        more.setVisibility(View.GONE);
                    }
                    receive_ll.setVisibility(View.VISIBLE);
                    receive_ll.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isDoubleClick()) {
                                return;
                            }
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("userId", item.getUserId() + "");
                            bundle1.putString("id", item.getId() + "");
                            bundle1.putString("module", "2");
                            bundle1.putString("name", item.getNickname());
                            bundle1.putString("content", item.getComment());
                            bundle1.putString("time", item.getCreateTime());
                            bundle1.putString("head", item.getAvatar());
                            bundle1.putString("praise", item.getLikeSum() + "");
                            bundle1.putString("commentNum", item.getCommentSum() + "");
                            bundle1.putString("contentId", mFindDetailBean.getDiscover().getId() + "");
                            bundle1.putString("content_userid", mFindDetailBean.getDiscover().getUserId() + "");
                            bundle1.putString("content_anonymity", /*mFindDetailBean.getDiscover().getAnonymity() +*/ "0");
                            bundle1.putString("replyCommentId", item.getId() + "");
                            bundle1.putString("isLike", item.getIsLike() + "");
                            CommentRemindJsonBean commentRemindJsonBean = new CommentRemindJsonBean();
                            List<RemindJson> remindJsons = GsonUtil.jsonToList(item.getRemindJson(), RemindJson.class);
                            commentRemindJsonBean.setList(remindJsons);
                            bundle1.putSerializable("commentRemindJsonBean", commentRemindJsonBean);
                            bundle1.putInt("weight", item.getWeight());
                            startActivity(CommentDetailActivity.class, bundle1);
                        }
                    });

                    XListView receive_listview = helper.getView(R.id.receive_listview);

                    QuickAdapter<CommentListBean.PageBean.ListBean.LowerCommentBean> child_adapter =
                            new QuickAdapter<CommentListBean.PageBean.ListBean.LowerCommentBean>(context, R.layout.item_receive, item.getLowerComment()) {
                                @Override
                                protected void convert(BaseAdapterHelper helper, CommentListBean.PageBean.ListBean.LowerCommentBean item) {
                                    TextView textView = helper.getView(R.id.textview);
                                    if (item.getUserId() == mFindDetailBean.getDiscover().getUserId()) {
                                        List<UserModel> list = new ArrayList<>();
                                        if (!TextUtils.isEmpty(item.getRemindJson()) && !"[]".equals(item.getRemindJson())) {
                                            List<RemindJson> remindJsons = GsonUtil.jsonToList(item.getRemindJson(), RemindJson.class);
                                            if (remindJsons != null) {
                                                for (int i = 0; i < remindJsons.size(); i++) {
                                                    UserModel userModel = new UserModel();
                                                    userModel.setUser_name(remindJsons.get(i).getNickname());
                                                    userModel.setUser_id(remindJsons.get(i).getUserId() + "");
                                                    list.add(userModel);
                                                }
                                            }
                                        }
                                        Spannable spannable = TextCommonUtils.getAtText(context, list,
                                                null,
                                                item.getNickname() + "(主人回复)：" + item.getComment(),
                                                getIText(textView),
                                                true,
                                                Color.parseColor(getString(R.string.remind_color)),
                                                0,
                                                spanAtUserCallBack,
                                                null);
                                        Spannable spannable1 = new SpannableString(spannable);
                                        spannable1.setSpan(span, 0, (item.getNickname() + "(主人回复)：").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                                        textView.setText(spannable1);
                                    } else {
                                        List<UserModel> list = new ArrayList<>();
                                        if (!TextUtils.isEmpty(item.getRemindJson()) && !"[]".equals(item.getRemindJson())) {
                                            List<RemindJson> remindJsons = GsonUtil.jsonToList(item.getRemindJson(), RemindJson.class);
                                            if (remindJsons != null) {
                                                for (int i = 0; i < remindJsons.size(); i++) {
                                                    UserModel userModel = new UserModel();
                                                    userModel.setUser_name(remindJsons.get(i).getNickname());
                                                    userModel.setUser_id(remindJsons.get(i).getUserId() + "");
                                                    list.add(userModel);
                                                }
                                            }
                                        }
                                        Spannable spannable = TextCommonUtils.getAtText(context, list,
                                                null,
                                                item.getNickname() + "回复" + item.getReplyNickname() + ":" + item.getComment(),
                                                getIText(textView),
                                                true,
                                                Color.parseColor(getString(R.string.remind_color)),
                                                0,
                                                spanAtUserCallBack,
                                                null);

                                        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(spannable);
                                        spannableStringBuilder.setSpan(span, 0, item.getNickname().length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                                        spannableStringBuilder.setSpan(span1, item.getNickname().length(), item.getNickname().length() + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                                        spannableStringBuilder.setSpan(span2, item.getNickname().length() + 2, item.getNickname().length() + 2 + item.getReplyNickname().length() + 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                                        textView.setText(spannableStringBuilder);
                                    }
                                }
                            };

                    receive_listview.setAdapter(child_adapter);
                    receive_listview.setFocusable(false);
                    receive_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("userId", item.getUserId() + "");
                            bundle1.putString("id", item.getId() + "");
                            bundle1.putString("module", "2");
                            bundle1.putString("name", item.getNickname());
                            bundle1.putString("content", item.getComment());
                            bundle1.putString("time", item.getCreateTime());
                            bundle1.putString("head", item.getAvatar());
                            bundle1.putString("praise", item.getLikeSum() + "");
                            bundle1.putString("commentNum", item.getCommentSum() + "");
                            bundle1.putString("contentId", mFindDetailBean.getDiscover().getId() + "");
                            bundle1.putString("content_userid", mFindDetailBean.getDiscover().getUserId() + "");
                            bundle1.putString("content_anonymity", /*mFindDetailBean.getDiscover().getAnonymity() +*/ "0");
                            bundle1.putString("replyCommentId", item.getId() + "");
                            bundle1.putString("isLike", item.getIsLike() + "");
                            CommentRemindJsonBean commentRemindJsonBean = new CommentRemindJsonBean();
                            List<RemindJson> remindJsons = GsonUtil.jsonToList(item.getRemindJson(), RemindJson.class);
                            commentRemindJsonBean.setList(remindJsons);
                            bundle1.putSerializable("commentRemindJsonBean", commentRemindJsonBean);
                            bundle1.putInt("weight", item.getWeight());
                            startActivity(CommentDetailActivity.class, bundle1);
                        }
                    });
                } else {
                    receive_ll.setVisibility(View.GONE);
                }
            }
        };

        listview.setAdapter(quickAdapter);

        listview.setFocusable(false);

    }

    private void deleteComment(CommentListBean.PageBean.ListBean item, String flag) {
        DialogUtil.getInstance().showCenterDialog(context, R.layout.dialog_more);
        LinearLayout dialog_more = (LinearLayout) DialogUtil.getInstance().dialog_center_layout;
        LinearLayout cancel_follow = dialog_more.findViewById(R.id.cancel_follow);
        LinearLayout delete = dialog_more.findViewById(R.id.delete);
        LinearLayout report = dialog_more.findViewById(R.id.report);
        LinearLayout black_list = dialog_more.findViewById(R.id.black_list);
        LinearLayout receive = dialog_more.findViewById(R.id.receive);

        TextView delete_tv = dialog_more.findViewById(R.id.delete_tv);
        TextView report_tv = dialog_more.findViewById(R.id.report_tv);

        delete_tv.setText("删除该评论");
        report_tv.setText("举报该评论");

        View line = dialog_more.findViewById(R.id.line);
        View viewline = dialog_more.findViewById(R.id.view);
        View view1 = dialog_more.findViewById(R.id.view1);
        View view2 = dialog_more.findViewById(R.id.view2);

        if (flag.equals("delete_report")) {
            report.setVisibility(View.VISIBLE);
            black_list.setVisibility(View.GONE);
            cancel_follow.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
            viewline.setVisibility(View.GONE);
            view1.setVisibility(View.VISIBLE);
            delete.setVisibility(View.VISIBLE);
            view2.setVisibility(View.VISIBLE);
            receive.setVisibility(View.VISIBLE);
        } else if ("report".equals(flag)) {
            report.setVisibility(View.VISIBLE);
            black_list.setVisibility(View.GONE);
            cancel_follow.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
            viewline.setVisibility(View.GONE);
            view1.setVisibility(View.INVISIBLE);
            delete.setVisibility(View.GONE);
            view2.setVisibility(View.VISIBLE);
            receive.setVisibility(View.VISIBLE);
        } else {
            report.setVisibility(View.GONE);
            black_list.setVisibility(View.GONE);
            cancel_follow.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
            viewline.setVisibility(View.GONE);
            view1.setVisibility(View.INVISIBLE);
            delete.setVisibility(View.VISIBLE);
            view2.setVisibility(View.VISIBLE);
            receive.setVisibility(View.VISIBLE);
        }

        receive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isDoubleClick()) {
                    return;
                }
                DialogUtil.getInstance().dialog_center.dismiss();
                replyUserId = item.getUserId() + "";
                replyCommentId = item.getId() + "";
                et_flag = "receive";
                replayName = item.getNickname();
                replayContent = item.getComment();
                tokenVerify();
            }
        });

        report.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isDoubleClick()) {
                    return;
                }
                DialogUtil.getInstance().dialog_center.dismiss();
                Bundle bundle = new Bundle();
                bundle.putString("flag", "report");
                bundle.putString("contentId", mFindDetailBean.getDiscover().getId() + "");
                bundle.putString("module", "2");
                bundle.putString("type", "2");
                bundle.putString("byUserId", item.getUserId() + "");
                bundle.putString("commentId", item.getId() + "");
                startActivity(ReportActivity.class, bundle);
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isDoubleClick()) {
                    return;
                }
                DialogUtil.getInstance().dialog_center.dismiss();
                showDialog(context, "删除中...");
                HttpParams params = new HttpParams();
                params.put("commentId", item.getId());
                finalOkGo.requestHead(RequestType.POSTHEAD, Url.DELETE_COMMENT, params, Url.DELETE_COMMENT, new Callback<SendCodeBean>() {
                    @Override
                    public void onSuccess(SendCodeBean sendCodeBean) {
                        dismissDialog();
                        if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                            if (!TextUtils.isEmpty(categoriesId)) {
                                EventBus.getDefault().post("delete_home_comment:" + categoriesId + ":" + position + ":" + item.getComment());
                            }
                            quickAdapter.remove(item);
                            quickAdapter.notifyDataSetChanged();
                            mFindDetailBean.getDiscover().setCommentSum(mFindDetailBean.getDiscover().getCommentSum() - 1);
                            comment_num.setText(mFindDetailBean.getDiscover().getCommentSum() + "");
                            comment_num_tv.setText(mFindDetailBean.getDiscover().getCommentSum() + "条评论");
                        } else {
                            if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                            } else {
                                showImageToas(sendCodeBean.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        dismissDialog();
                    }
                });
            }
        });
    }


    private long formattingtime;
    private long TimeCha;

    private void timecha(String startime, TextView textView) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dataformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long currenttime = calendar.getTimeInMillis();//获取系统当先时间的毫秒值
        try {//如果你的初始时间是格式化过的,则需要先转化成毫秒值
            formattingtime = dataformat.parse(startime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
//这里需要注意,因为获取的时间因为时差的缘故已经自动加上了8小时的时间,所以这里需要减去.如果不减去,就算是你刚刚发布的动态也会显示是8小时之前
        TimeCha = ((currenttime - formattingtime) / 1000) /*- (8 * 60 * 60)*/;//将毫秒值转化成分钟数
//        Logger.e(TimeCha+"");
        if (TimeCha < 60) {
            textView.setText("刚刚");
        } else if (TimeCha >= 60 && TimeCha < 3600) {
            textView.setText(TimeCha / 60 + "分钟前");
        } else if (TimeCha >= 3600 && TimeCha < 3600 * 24) {
            textView.setText(TimeCha / 3600 + "小时前");
        } else if (TimeCha >= 3600 * 1 * 24 && TimeCha < 3600 * 31 * 24) {
            textView.setText(TimeCha / 3600 / 24 + "天前");
        } else if (TimeCha >= 3600 * 31 * 24 * 1 && TimeCha < 3600 * 31 * 24 * 12) {
            textView.setText(TimeCha / 3600 / 24 / 30 + "个月前");
        } else {
            textView.setText(TimeCha / 3600 / 24 / 30 / 12 + "年前");
        }
    }

    @Override
    protected void initData() {
        showDialog(this, null);
        HttpParams params1 = new HttpParams();
        params1.put("id", id);
        if (mUserInfoBean != null) {
            params1.put("userId", mUserInfoBean.getUser().getUserId());
        }
        finalOkGo.request(RequestType.POST, Url.FIND_DETAIL, params1, Url.FIND_DETAIL, new Callback<FindDetailBean>() {
            @Override
            public void onSuccess(FindDetailBean findDetailBean) {
                if (findDetailBean.getCode() == Contacts.SUCCESS) {
                    mFindDetailBean = findDetailBean;
                    name.setText(findDetailBean.getDiscover().getNickname());
                    timecha(findDetailBean.getDiscover().getCreateTime(), time);

                    String varjs = "<script type='text/javascript'> \nwindow.onload = function()\n{var $video = document.getElementsByTagName('video');for(var p in  $video){$video[p].style.width = '100%'; $video[p].style.height ='auto'; var $img = document.getElementsByTagName('img');for(var p in  $video){$img[p].style.width = '100%'; $img[p].style.height ='auto';}}</script>";

                    webview.loadDataWithBaseURL(null, varjs + findDetailBean.getDiscover().getContent().replace("<img", "<img style=\"display:        ;max-width:100%;\""), "text/html", "UTF-8", null);

                    Glide.with(context)
                            .load(findDetailBean.getDiscover().getAvatar() + Config.IMAGE_LIM)
                            .dontAnimate()
                            .placeholder(R.drawable.icon_no_head)
                            .into(headPhoto);

                    Glide.with(context)
                            .load(findDetailBean.getDiscover().getCoverImage() + Config.IMAGE_LIM_)
                            .dontAnimate()
                            .placeholder(R.color.color_f3f3)
                            .error(R.color.color_f3f3)
                            .into(findBg);

//                    Picasso.get().load(findDetailBean.getDiscover().getCoverImage() + Config.IMAGE_LIM)
//                            .error(R.color.color_f3f3)
//                            .into(findBg);

                    praise_num.setText(findDetailBean.getDiscover().getLikeSum() + "");

                    comment_num.setText(findDetailBean.getDiscover().getCommentSum() + "");

                    share_num.setText(findDetailBean.getDiscover().getShareSum() + "");

                    comment_num_tv.setText(findDetailBean.getDiscover().getCommentSum() + "条评论");

                    if (mUserInfoBean != null) {
                        if (mUserInfoBean.getUser().getUserId() == findDetailBean.getDiscover().getUserId()) {
                            follow.setVisibility(View.INVISIBLE);
                        } else {
                            follow.setVisibility(View.VISIBLE);
                            if (findDetailBean.getDiscover().getIsAttention() == 1) {
                                follow.setText("已关注");
                            } else {
                                follow.setText("关注");
                            }
                        }
                    } else {
                        follow.setVisibility(View.VISIBLE);
                        if (findDetailBean.getDiscover().getIsAttention() == 1) {
                            follow.setText("已关注");
                        } else {
                            follow.setText("关注");
                        }
                    }

                    if (findDetailBean.getDiscover().getIsLike() == 1) {
                        praiseIv.setImageResource(R.mipmap.icon_praise_select);
                    } else {
                        praiseIv.setImageResource(R.mipmap.item_praise);
                    }

                    requestComment(findDetailBean);
                } else {
                    dismissDialog();
                    if (findDetailBean.getCode() == Contacts.TOKEN_EMPTY) {
                    } else {
                        showImageToas(findDetailBean.getMsg());
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    private void requestData() {
        showDialog(this, null);
        HttpParams params1 = new HttpParams();
        params1.put("id", id);
        if (mUserInfoBean != null) {
            params1.put("userId", mUserInfoBean.getUser().getUserId());
        }
        finalOkGo.request(RequestType.POST, Url.FIND_DETAIL, params1, Url.FIND_DETAIL, new Callback<FindDetailBean>() {
            @Override
            public void onSuccess(FindDetailBean findDetailBean) {
                if (findDetailBean.getCode() == Contacts.SUCCESS) {
                    mFindDetailBean = findDetailBean;

                    praise_num.setText(findDetailBean.getDiscover().getLikeSum() + "");

                    comment_num.setText(findDetailBean.getDiscover().getCommentSum() + "");

                    share_num.setText(findDetailBean.getDiscover().getShareSum() + "");

                    comment_num_tv.setText(findDetailBean.getDiscover().getCommentSum() + "条评论");

                    if (mUserInfoBean != null) {
                        if (mUserInfoBean.getUser().getUserId() == findDetailBean.getDiscover().getUserId()) {
                            follow.setVisibility(View.INVISIBLE);
                        } else {
                            follow.setVisibility(View.VISIBLE);
                            if (findDetailBean.getDiscover().getIsAttention() == 1) {
                                follow.setText("已关注");
                            } else {
                                follow.setText("关注");
                            }
                        }
                    } else {
                        follow.setVisibility(View.VISIBLE);
                        if (findDetailBean.getDiscover().getIsAttention() == 1) {
                            follow.setText("已关注");
                        } else {
                            follow.setText("关注");
                        }
                    }

                    if (findDetailBean.getDiscover().getIsLike() == 1) {
                        praiseIv.setImageResource(R.mipmap.icon_praise_select);
                    } else {
                        praiseIv.setImageResource(R.mipmap.item_praise);
                    }

                    requestComment(findDetailBean);
                } else {
                    dismissDialog();
                    if (findDetailBean.getCode() == Contacts.TOKEN_EMPTY) {
                    } else {
                        showImageToas(findDetailBean.getMsg());
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    private void requestComment(FindDetailBean findDetailBean) {
        HttpParams params = new HttpParams();
        params.put("contentId", findDetailBean.getDiscover().getId());
        params.put("type", 2);
        if (mUserInfoBean != null) {
            params.put("userId", mUserInfoBean.getUser().getUserId());
        }
        params.put("position", 1);
        params.put("limit", pageSize);
        params.put("page", pageNum++);
        finalOkGo.request(RequestType.POST, Url.GET_COMMENT_LIST, params, Url.GET_COMMENT_LIST, new Callback<CommentListBean>() {
            @Override
            public void onSuccess(CommentListBean commentListBean) {
                dismissDialog();
                if (commentListBean.getCode() == Contacts.SUCCESS) {
                    if (pageNum - 1 == 1) {
                        quickAdapter.clear();
                        quickAdapter.addAll(commentListBean.getPage().getList());
                    } else {
                        quickAdapter.addAll(commentListBean.getPage().getList());
                    }

                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                    quickAdapter.notifyDataSetChanged();

                    int pagecount;
                    if (commentListBean.getPage().getTotalCount() % pageSize == 0) {
                        pagecount = commentListBean.getPage().getTotalCount() / pageSize;
                    } else {
                        pagecount = commentListBean.getPage().getTotalCount() / pageSize + 1;
                    }

                    if (pageNum > pagecount) {
                        refreshLayout.setNoMoreData(true);
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.setNoMoreData(false);
                    }

                    if (!TextUtils.isEmpty(flag)) {
                        if (flag.equals("comment") && !isFirst) {
                            isFirst = true;
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    scrollView.scrollTo(0, comment_num_tv.getTop());
                                }
                            }, 1500);
                        }
                    }

                } else {
                    if (commentListBean.getCode() == Contacts.TOKEN_EMPTY) {
                    } else {
                        showImageToas(commentListBean.getMsg());
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    //    @Override
    @OnClick({R.id.comment_et, R.id.head_photo, R.id.name, R.id.follow, R.id.add, R.id.send, R.id.praise, R.id.comment_ll, R.id.share})
    public void onViewClicked(View view) {
//        super.onViewClicked(view);
        if (Utils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.head_photo:
                if (mFindDetailBean != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("userId", mFindDetailBean.getDiscover().getUserId() + "");
                    bundle.putInt("position", position);
                    bundle.putString("categoriesId", categoriesId);
                    startActivity(FindOtherInfoActivity.class, bundle);
                }
                break;
            case R.id.name:
                if (mFindDetailBean != null) {
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("userId", mFindDetailBean.getDiscover().getUserId() + "");
                    bundle1.putInt("position", position);
                    bundle1.putString("categoriesId", categoriesId);
                    startActivity(FindOtherInfoActivity.class, bundle1);
                }
                break;
            case R.id.follow:
                if (mFindDetailBean != null) {
                    if (mFindDetailBean.getDiscover().getIsAttention() == 0) {
                        showDialog(context, "关注中...");
                        finalOkGo.requestHead(RequestType.GETHEAD, Url.FOLLOW_OTHER + mFindDetailBean.getDiscover().getUserId(), null, Url.FOLLOW_OTHER, new Callback<SendCodeBean>() {
                            @Override
                            public void onSuccess(SendCodeBean sendCodeBean) {
                                dismissDialog();
                                if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                    mFindDetailBean.getDiscover().setIsAttention(1);
                                    follow.setText("已关注");
                                    EventBus.getDefault().post("refresh_find_list_follow");
                                    EventBus.getDefault().post("refresh_detail_follow:" + categoriesId + ":" + mFindDetailBean.getDiscover().getUserId());
                                    EventBus.getDefault().post("refresh_mine");
                                } else {
                                    if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                    } else {
                                        showImageToas(sendCodeBean.getMsg());
                                    }
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                dismissDialog();
                            }
                        });
                    } else {
                        showDialog(context, "取消关注...");
                        finalOkGo.requestHead(RequestType.GETHEAD, Url.CANCEL_FOLLOW + mFindDetailBean.getDiscover().getUserId(), null, Url.CANCEL_FOLLOW, new Callback<SendCodeBean>() {
                            @Override
                            public void onSuccess(SendCodeBean sendCodeBean) {
                                dismissDialog();
                                if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                    mFindDetailBean.getDiscover().setIsAttention(0);
                                    follow.setText("关注");
                                    EventBus.getDefault().post("refresh_find_list_cancel_follow");
                                    EventBus.getDefault().post("refresh_detail_cancel_follow:" + categoriesId + ":" + mFindDetailBean.getDiscover().getUserId());
                                    EventBus.getDefault().post("refresh_mine");
                                } else {
                                    if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                    } else {
                                        showImageToas(sendCodeBean.getMsg());
                                    }
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                dismissDialog();
                            }
                        });
                    }
                }
                break;
            case R.id.add:
                break;
            case R.id.send:
                et_flag = "comment";
                replayName = "";
                replayContent = "";
                tokenVerify();
                break;
            case R.id.comment_et:
                et_flag = "comment";
                replayName = "";
                replayContent = "";
                tokenVerify();
                break;
            case R.id.praise:
                if (mFindDetailBean != null) {
                    if (mFindDetailBean.getDiscover().getIsLike() == 1) {
                        praise.setEnabled(false);
                        showDialog(context, "取消中...");
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("contentId", mFindDetailBean.getDiscover().getId());
                            jsonObject.put("module", 2);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.CANCEL_PRAISE_HOME, null, jsonObject.toString(), new Callback<SendCodeBean>() {
                            @Override
                            public void onSuccess(SendCodeBean sendCodeBean) {
                                dismissDialog();
                                praise.setEnabled(true);
                                if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                    mFindDetailBean.getDiscover().setIsLike(0);
                                    mFindDetailBean.getDiscover().setLikeSum(mFindDetailBean.getDiscover().getLikeSum() - 1);
                                    praise_num.setText(mFindDetailBean.getDiscover().getLikeSum() + "");
                                    praiseIv.setImageResource(R.mipmap.item_praise);
                                    if (!TextUtils.isEmpty(categoriesId)) {
                                        EventBus.getDefault().post("refresh_home_cancel_praise:" + categoriesId + ":" + position);
                                    }
                                    EventBus.getDefault().post("refresh_find_cancel_praise:" + mFindDetailBean.getDiscover().getId());
                                } else {
                                    if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                    } else {
                                        showImageToas(sendCodeBean.getMsg());
                                    }
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                dismissDialog();
                                praise.setEnabled(true);
                            }
                        });
                    } else {
                        praise.setEnabled(false);
                        showDialog(context, "点赞中...");
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("contentId", mFindDetailBean.getDiscover().getId());
                            jsonObject.put("module", 2);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.PRAISE_HOME, null, jsonObject.toString(), new Callback<SendCodeBean>() {
                            @Override
                            public void onSuccess(SendCodeBean sendCodeBean) {
                                dismissDialog();
                                praise.setEnabled(true);
                                if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                    mFindDetailBean.getDiscover().setIsLike(1);
                                    mFindDetailBean.getDiscover().setLikeSum(mFindDetailBean.getDiscover().getLikeSum() + 1);
                                    praise_num.setText(mFindDetailBean.getDiscover().getLikeSum() + "");
                                    praiseIv.setImageResource(R.mipmap.icon_praise_select);
                                    if (!TextUtils.isEmpty(categoriesId)) {
                                        EventBus.getDefault().post("refresh_home_praise:" + categoriesId + ":" + position);
                                    }
                                    EventBus.getDefault().post("refresh_find_praise:" + mFindDetailBean.getDiscover().getId());
                                } else {
                                    if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                    } else {
                                        showImageToas(sendCodeBean.getMsg());
                                    }
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                dismissDialog();
                                praise.setEnabled(true);
                            }
                        });
                    }
                }
                break;
            case R.id.comment_ll:
                et_flag = "comment";
                replayName = "";
                replayContent = "";
                tokenVerify();
                break;
            case R.id.share:
                if (mFindDetailBean != null) {
                    showDialog(context, null);
                    JSONObject jsonObject1 = new JSONObject();
                    try {
                        jsonObject1.put("contentId", mFindDetailBean.getDiscover().getId());
                        jsonObject1.put("module", 2);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.SHARE_DATA, null, jsonObject1.toString(), new Callback<ShareFindBean>() {
                        @Override
                        public void onSuccess(ShareFindBean shareDataBean) {
                            dismissDialog();
                            if (shareDataBean.getCode() == Contacts.SUCCESS) {
                                showShareDialog(shareDataBean, "share");
                            } else {
                                if (shareDataBean.getCode() == Contacts.TOKEN_EMPTY) {
                                } else {
                                    showImageToas(shareDataBean.getMsg());
                                }
                            }

                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            dismissDialog();
                        }
                    });
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void OnMessageEvent(String event) {
        String s = event.split(":")[0];
        if (s.equals("FindDetailActivity_comment")) {
            if (!TextUtils.isEmpty(commentEt.getText().toString().trim())) {
                if (et_flag.equals("comment")) {
                    showDialog(context, "评论中...");
                    JSONObject jsonObject = new JSONObject();
                    JSONArray jsonArray = new JSONArray();
                    try {
                        for (int i = 0; i < commentEt.getNameList().size(); i++) {
                            JSONObject userJson = new JSONObject();
                            userJson.put("userId", commentEt.getNameList().get(i).getUser_id());
                            userJson.put("nickname", commentEt.getNameList().get(i).getUser_name());
                            jsonArray.put(userJson);
                        }
                        jsonObject.put("remindJson", jsonArray.toString());
                        jsonObject.put("contentId", mFindDetailBean.getDiscover().getId());
                        jsonObject.put("comment", commentEt.getText().toString().trim());
                        jsonObject.put("position", 1);
                        jsonObject.put("type", 2);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.COMMENT_SAVE, null, jsonObject.toString(), new Callback<SendCodeBean>() {
                        @Override
                        public void onSuccess(SendCodeBean sendCodeBean) {
                            dismissDialog();
                            if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                if (!TextUtils.isEmpty(categoriesId)) {
                                    EventBus.getDefault().post("refresh_home_comment:" + categoriesId + ":" + position + ":" + commentEt.getText().toString().trim());
                                }
                                EventBus.getDefault().post("refresh_find_comment_count:" + mFindDetailBean.getDiscover().getId());
                                replayName = "";
                                replayContent = "";
                                mFindDetailBean.getDiscover().setCommentSum(mFindDetailBean.getDiscover().getCommentSum() + 1);
                                comment_num.setText(mFindDetailBean.getDiscover().getCommentSum() + "");
                                comment_num_tv.setText(mFindDetailBean.getDiscover().getCommentSum() + "条评论");
                                if (mFindDetailBean != null) {
                                    List<CommentListBean.PageBean.ListBean> commentList = new ArrayList<>();
                                    for (int i = 0; i < quickAdapter.getCount(); i++) {
                                        commentList.add(quickAdapter.getItem(i));
                                    }
                                    CommentListBean.PageBean.ListBean bean = new CommentListBean.PageBean.ListBean();
                                    bean.setId(sendCodeBean.getCommentId());
                                    bean.setComment(commentEt.getText().toString().trim());
                                    bean.setContentId(mFindDetailBean.getDiscover().getId());
                                    bean.setPosition(1);
                                    bean.setType(2);
                                    bean.setRemindJson(jsonArray.toString());
                                    bean.setCreateTime(DateFormatUtil.ms4Date(System.currentTimeMillis()));
                                    bean.setAvatar(mUserInfoBean.getUser().getAvatar());
                                    bean.setNickname(mUserInfoBean.getUser().getNickname());
                                    bean.setUserId(mUserInfoBean.getUser().getUserId());
                                    quickAdapter.clear();
                                    quickAdapter.add(bean);
                                    quickAdapter.addAll(commentList);
                                    commentEt.setText("");
                                    scrollView.scrollTo(0, comment_num_tv.getTop());
                                }
                            } else {
                                if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                } else {
                                    showImageToas(sendCodeBean.getMsg());
                                }
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            dismissDialog();
                        }
                    });
                } else {
                    showDialog(context, "评论中...");
                    JSONObject jsonObject = new JSONObject();
                    JSONArray jsonArray = new JSONArray();
                    try {
                        for (int i = 0; i < commentEt.getNameList().size(); i++) {
                            JSONObject userJson = new JSONObject();
                            userJson.put("userId", commentEt.getNameList().get(i).getUser_id());
                            userJson.put("nickname", commentEt.getNameList().get(i).getUser_name());
                            jsonArray.put(userJson);
                        }
                        jsonObject.put("remindJson", jsonArray.toString());
                        jsonObject.put("contentId", mFindDetailBean.getDiscover().getId());
                        jsonObject.put("comment", commentEt.getText().toString().trim());
                        jsonObject.put("replyUserId", replyUserId);
                        jsonObject.put("replyCommentId", replyCommentId);
                        jsonObject.put("position", 2);
                        jsonObject.put("type", 2);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.COMMENT_SAVE, null, jsonObject.toString(), new Callback<SendCodeBean>() {
                        @Override
                        public void onSuccess(SendCodeBean sendCodeBean) {
                            dismissDialog();
                            if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                et_flag = "comment";
                                if (!TextUtils.isEmpty(categoriesId)) {
                                    EventBus.getDefault().post("refresh_home_comment_count:" + categoriesId + ":" + position);
                                }
                                EventBus.getDefault().post("refresh_find_comment_count:" + mFindDetailBean.getDiscover().getId());
                                replayName = "";
                                replayContent = "";
                                mFindDetailBean.getDiscover().setCommentSum(mFindDetailBean.getDiscover().getCommentSum() + 1);
                                comment_num.setText(mFindDetailBean.getDiscover().getCommentSum() + "");
                                comment_num_tv.setText(mFindDetailBean.getDiscover().getCommentSum() + "条评论");

                                if (quickAdapter.getItem(replyPosition).getLowerComment() == null) {
                                    quickAdapter.getItem(replyPosition).setLowerComment(new LinkedList<CommentListBean.PageBean.ListBean.LowerCommentBean>());
                                }
                                CommentListBean.PageBean.ListBean.LowerCommentBean lowerCommentBean = new CommentListBean.PageBean.ListBean.LowerCommentBean();
                                lowerCommentBean.setComment(commentEt.getText().toString().trim());
                                lowerCommentBean.setContentId(mFindDetailBean.getDiscover().getId());
                                lowerCommentBean.setRemindJson(jsonArray.toString());
                                lowerCommentBean.setReplyUserId(Integer.parseInt(replyUserId));
                                lowerCommentBean.setReplyCommentId(Integer.parseInt(replyCommentId));
                                lowerCommentBean.setNickname(mUserInfoBean.getUser().getNickname());
                                lowerCommentBean.setReplyNickname(quickAdapter.getItem(replyPosition).getNickname());
                                lowerCommentBean.setUserId(mUserInfoBean.getUser().getUserId());
                                lowerCommentBean.setReplyUserId(quickAdapter.getItem(replyPosition).getUserId());
                                if (quickAdapter.getItem(replyPosition).getLowerComment().size() >= 2) {
                                    quickAdapter.getItem(replyPosition).getLowerComment().set(0, lowerCommentBean);
                                } else {
                                    quickAdapter.getItem(replyPosition).getLowerComment().addFirst(lowerCommentBean);
                                }
                                quickAdapter.getItem(replyPosition).setCommentSum(quickAdapter.getItem(replyPosition).getCommentSum() + 1);
                                quickAdapter.notifyDataSetChanged();
                                commentEt.setText("");
                            } else {
                                if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                } else {
                                    showImageToas(sendCodeBean.getMsg());
                                }
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            dismissDialog();
                        }
                    });
                }
            } else {
//                showImageToas("请填写点内容吧");
            }
        } else if (s.equals("refresh_praise_comment_count")) {
            for (int i = 0; i < quickAdapter.getCount(); i++) {
                if ((quickAdapter.getItem(i).getId() + "").equals(event.split(":")[1])) {
                    quickAdapter.getItem(i).setIsLike(1);
                    quickAdapter.getItem(i).setLikeSum(quickAdapter.getItem(i).getLikeSum() + 1);
                    quickAdapter.notifyDataSetChanged();
                }
            }
        } else if (s.equals("refresh_cancel_praise_comment_count")) {
            for (int i = 0; i < quickAdapter.getCount(); i++) {
                if ((quickAdapter.getItem(i).getId() + "").equals(event.split(":")[1])) {
                    quickAdapter.getItem(i).setIsLike(0);
                    quickAdapter.getItem(i).setLikeSum(quickAdapter.getItem(i).getLikeSum() - 1);
                    quickAdapter.notifyDataSetChanged();
                }
            }
        } else if (s.equals("comment_detial_add")) {
            mFindDetailBean.getDiscover().setCommentSum(mFindDetailBean.getDiscover().getCommentSum() + 1);
            comment_num.setText(mFindDetailBean.getDiscover().getCommentSum() + "");
            comment_num_tv.setText(mFindDetailBean.getDiscover().getCommentSum() + "条评论");
            if (mFindDetailBean != null) {
                pageNum = 1;
                requestComment(mFindDetailBean);
            }
        } else if (s.equals("comment_detial_sub")) {
            mFindDetailBean.getDiscover().setCommentSum(mFindDetailBean.getDiscover().getCommentSum() - 1);
            comment_num.setText(mFindDetailBean.getDiscover().getCommentSum() + "");
            comment_num_tv.setText(mFindDetailBean.getDiscover().getCommentSum() + "条评论");
            if (mFindDetailBean != null) {
                pageNum = 1;
                requestComment(mFindDetailBean);
            }
        } else if (s.equals("refresh_find_detail_follow")) {
            mFindDetailBean.getDiscover().setIsAttention(1);
            follow.setText("已关注");
        } else if (s.equals("refresh_find_detail_cancel_follow")) {
            mFindDetailBean.getDiscover().setIsAttention(0);
            follow.setText("关注");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        RichText.clear(this);
        if (webview != null) {
            webview.destroy();
        }
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if (mFindDetailBean != null) {
            requestComment(mFindDetailBean);
        } else {
            refreshLayout.finishLoadMore();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        requestData();

    }

    private void showShareDialog(ShareFindBean shareDataBean, String flag) {

        DialogUtil.getInstance().showBottomDialog(context, R.layout.dialog_share);
        LinearLayout dialog_share = (LinearLayout) DialogUtil.getInstance().dialog_bottom_layout;
        dialog_share.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isDoubleClick()) {
                    return;
                }
                DialogUtil.getInstance().dialog_bottom.dismiss();
            }
        });

        dialog_share.findViewById(R.id.share_wechat).setOnClickListener(view -> {
            if (Utils.isDoubleClick()) {
                return;
            }
            DialogUtil.getInstance().dialog_bottom.dismiss();
            WXShareUtil wxShareUtil = new WXShareUtil(context);
            wxShareUtil.registerWx();
            WechatLoginUtil.getInstance().setWechatCallBack(FindDetailActivity.this);
            wxShareUtil.share(shareDataBean.getUrl(),
                    shareDataBean.getDiscover().getTitle(),
                    "",
                    shareDataBean.getDiscover().getImage() + "?imageView2/2/w/100", wxShareUtil.SHARETOFRIEND);
        });

        dialog_share.findViewById(R.id.share_wechat_friend).setOnClickListener(view -> {
            if (Utils.isDoubleClick()) {
                return;
            }
            DialogUtil.getInstance().dialog_bottom.dismiss();
            WXShareUtil wxShareUtil = new WXShareUtil(context);
            wxShareUtil.registerWx();
            WechatLoginUtil.getInstance().setWechatCallBack(FindDetailActivity.this);
            wxShareUtil.share(shareDataBean.getUrl(),
                    shareDataBean.getDiscover().getTitle(),
                    "",
                    shareDataBean.getDiscover().getImage() + "?imageView2/2/w/100", wxShareUtil.SHARETOPYQ);
        });

        dialog_share.findViewById(R.id.share_qq).setOnClickListener(view -> {
            if (Utils.isDoubleClick()) {
                return;
            }
            DialogUtil.getInstance().dialog_bottom.dismiss();
            mTencent = Tencent.createInstance(Config.QQ_APP_ID, ContextUtil.getContext());
            Bundle params = new Bundle();
            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
            params.putString(QQShare.SHARE_TO_QQ_TITLE, shareDataBean.getDiscover().getTitle());
            params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "");
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareDataBean.getDiscover().getImage() + "?imageView2/2/w/100");
            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareDataBean.getUrl());
            mTencent.shareToQQ(FindDetailActivity.this, params, new BaseUiListener());
            shareSuccess();
        });

        dialog_share.findViewById(R.id.share_qq_space).setOnClickListener(view -> {
            if (Utils.isDoubleClick()) {
                return;
            }
            DialogUtil.getInstance().dialog_bottom.dismiss();
            mTencent = Tencent.createInstance(Config.QQ_APP_ID, context);
            Bundle params = new Bundle();
            params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
            params.putString(QzoneShare.SHARE_TO_QQ_TITLE, shareDataBean.getDiscover().getTitle());//选填
            params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "");
            params.putString(QzoneShare.SHARE_TO_QQ_IMAGE_URL, shareDataBean.getDiscover().getImage() + "?imageView2/2/w/100");
            params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, shareDataBean.getUrl());
            params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, new ArrayList<String>());
            mTencent.shareToQzone(FindDetailActivity.this, params, new BaseUiListener());
            shareSuccess();
        });

        dialog_share.findViewById(R.id.share_weibo).setOnClickListener(view -> {
            if (Utils.isDoubleClick()) {
                return;
            }
            DialogUtil.getInstance().dialog_bottom.dismiss();
            WbSdk.install(ContextUtil.getContext(), new AuthInfo(ContextUtil.getContext(), Config.SINA_APP_KEY, Config.REDIRECT_URL, ""));
            WbShareHandler shareHandler = new WbShareHandler(FindDetailActivity.this);
            shareHandler.registerApp();
            WeiboMultiMessage weiboMessage = new WeiboMultiMessage();
            weiboMessage.textObject = getTextObj(shareDataBean.getUrl(), shareDataBean.getDiscover().getTitle());
            shareHandler.shareMessage(weiboMessage, false);
            shareSuccess();
        });

        dialog_share.findViewById(R.id.copy_link).setOnClickListener(view -> {
            if (Utils.isDoubleClick()) {
                return;
            }
            DialogUtil.getInstance().dialog_bottom.dismiss();
            ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
            ClipData clipData = ClipData.newPlainText(null, shareDataBean.getUrl());
            // 把数据集设置（复制）到剪贴板
            clipboard.setPrimaryClip(clipData);
            showToast("链接已复制");
        });


        if (flag.equals("share")) {
            dialog_share.findViewById(R.id.black_list).setVisibility(View.INVISIBLE);
            dialog_share.findViewById(R.id.delete).setVisibility(View.INVISIBLE);
            dialog_share.findViewById(R.id.report).setVisibility(View.INVISIBLE);
            dialog_share.findViewById(R.id.copy_text).setVisibility(View.INVISIBLE);
        } else {
            dialog_share.findViewById(R.id.black_list).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isDoubleClick()) {
                        return;
                    }
                    showDialog(context, "屏蔽中...");
                    finalOkGo.requestHead(RequestType.POSTHEAD, Url.ADD_BLACK_LIST + mFindDetailBean.getDiscover().getUserId(), null, Url.ADD_BLACK_LIST, new Callback<SendCodeBean>() {
                        @Override
                        public void onSuccess(SendCodeBean sendCodeBean) {
                            dismissDialog();
                            if (sendCodeBean.getCode() == Contacts.SUCCESS) {
//                                EventBus.getDefault().post("black_list:" + mFindDetailBean.getDiscover().getUserId());
                                if (!TextUtils.isEmpty(categoriesId)) {
                                    EventBus.getDefault().post("black_detail_list:" + categoriesId + ":" + mFindDetailBean.getDiscover().getUserId());
                                }
                                finishActivity();
                            } else {
                                if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                } else {
                                    showImageToas(sendCodeBean.getMsg());
                                }
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            dismissDialog();
                        }
                    });
                }
            });
            dialog_share.findViewById(R.id.report).setOnClickListener(view -> {
                if (Utils.isDoubleClick()) {
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString("flag", "report");
                bundle.putString("contentId", mFindDetailBean.getDiscover().getId() + "");
                bundle.putString("module", "2");
                bundle.putString("type", "1");
                bundle.putString("byUserId", mFindDetailBean.getDiscover().getUserId() + "");
                startActivity(ReportActivity.class, bundle);
            });

        }

    }

    @Override
    public void onWbShareSuccess() {
//        showToast("分享成功");
//        shareSuccess();
    }

    @Override
    public void onWbShareCancel() {
//        showImageToas("分享取消");
    }

    @Override
    public void onWbShareFail() {
//        showImageToas("分享失败");
    }

    @Override
    public void callBack(BaseResp baseResp) {
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                showToast("分享成功");
                shareSuccess();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                showImageToas("分享取消");
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                showImageToas("分享被拒绝");
                break;
            default:
                showImageToas("分享返回");
                break;
        }
    }

    private class BaseUiListener implements IUiListener {

        @Override
        public void onComplete(Object o) {
//            showToast("分享成功");
//            shareSuccess();
        }

        @Override
        public void onError(UiError uiError) {
//            showImageToas("分享失败");
        }

        @Override
        public void onCancel() {
//            showImageToas("分享取消");
        }
    }

    private void shareSuccess() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("contentId", mFindDetailBean.getDiscover().getId());
            jsonObject.put("module", 2);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.SHARE_SUCCESS, null, jsonObject.toString(), new Callback<SendCodeBean>() {
            @Override
            public void onSuccess(SendCodeBean sendCodeBean) {
                if (sendCodeBean.getCode() == Contacts.SUCCESS) {

                } else {
                    if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                    } else {
                        showImageToas(sendCodeBean.getMsg());
                    }
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mTencent != null) {
            mTencent.onActivityResult(requestCode, resultCode, data);
        }
    }


    private TextObject getTextObj(String url, String title) {
        TextObject textObject = new TextObject();
        textObject.text = title + "\n" + url;
        textObject.title = title;
        textObject.actionUrl = url;
        return textObject;
    }

}
